﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sinacor.Infra.UI.Controls.SmartClient;
using Sinacor.UI.Bovespa.Contabil.Presenter;
using Sinacor.Infra.UI.Common;

namespace Sinacor.UI.Bovespa.Contabil.View.SmartClient
{
    /// <summary>
    /// Interaction logic for PesquisaHistoricosContabilidade.xaml
    /// </summary>
    public partial class PesquisaHistoricosContabilidade : SinacorWindow, IPesquisaHistoricosContabeis
    {
        private Presenter.PesquisaHistoricosContabeis _presenter;
        private Model.HistoricoContabil historico = null;

        public Model.HistoricoContabil Historico
        {
            get { return historico; }
            set { historico = value; }
        }


        /// <summary>
        /// FGualberto.7COMm - Task: 6312
        /// </summary>
        public PesquisaHistoricosContabilidade()
        {
            InitializeComponent();
            
            _presenter = new Presenter.PesquisaHistoricosContabeis(this);
            this.DataContext = _presenter;
            historico = new Model.HistoricoContabil();
            dtgHistorico.NewItemType = typeof(Model.HistoricoContabil);
            this.AfterInitialize += new RoutedEventHandler(PesquisaHistoricosContabilidade_AfterInitialize);
        }

        public PesquisaHistoricosContabilidade(Model.HistoricoContabil historico)
        {
            // Este deve ser o primeiro método do contrutor da View
            InitializeComponent();

            // Inicialize a presenter no construtor
            _presenter = new Presenter.PesquisaHistoricosContabeis(this);
            this.DataContext = _presenter;

            this._presenter.FiltroPesquisa.Codigo = historico.Codigo;
            this._presenter.FiltroPesquisa.Descricao = historico.Descricao;
            this._presenter.FiltroPesquisa.Digito = historico.Digito;

            dtgHistorico.NewItemType = typeof(Model.HistoricoContabil);

            this.AfterInitialize += new RoutedEventHandler(PesquisaHistoricosContabilidade_AfterInitialize);
        }

        void PesquisaHistoricosContabilidade_AfterInitialize(object sender, RoutedEventArgs e)
        {
            this._presenter.Pesquisar();
        }

        /// <summary>
        /// Pesquisar
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPesquisar_Click(object sender, RoutedEventArgs e)
        {
            this.ShowUserWaitingProcess();
            _presenter.Pesquisar();
            this.HideUserWaitingProcess();
        }

        #region IPesquisaHistoricosContabeis Members

        /// <summary>
        /// Atualizar Histórico Contabil
        /// </summary>
        public void AtualizarHistoricoContabil()
        {            
            dtgHistorico.UpdateTarget(SinacorDataGrid.ItemsSourceProperty);
        }

        #endregion

        /// <summary>
        /// Limpar Valores Tela
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLimpar_Click(object sender, RoutedEventArgs e)
        {
            _presenter.ListaHistorico.Clear(); 
            _presenter.FiltroPesquisa.Codigo = null;
            _presenter.FiltroPesquisa.Descricao= null;
        }

        /// <summary>
        /// Carregar Valor Selecionado
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dtgHistorico_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            historico = (Model.HistoricoContabil)((System.Windows.Controls.Primitives.MultiSelector)(sender)).SelectedItem;
        }

        private void dtgHistorico_ConfirmSelection(object sender, ItemEventArgs e)
        {
            if (this.dtgHistorico.SelectedItems.Count > 0)
            {
                this.Historico = ((Model.HistoricoContabil)dtgHistorico.Items.CurrentItem);
                this.Close();
            }
            e.Handled = true;
        }
    }


}
